// pages/confirmorder/pay.js
import Router from '../../utils/router.js';
import base from '../../config/base.js';
import service from '../../services/confirmorder';
import Record from '../../services/record-stat';
import util from '../../utils/util.js';
import shareInfoJson from '../../utils/hbshare.js';
import rewardsj from '../../services/rewardsj';
import service_store from '../../services/store';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    icon_play: base.static + 'store/play.png',//视频标志
    hottodyList: '',
    scrollTop: '',
    orderid: '',//订单id
    run_num: '',//订单号
    nomore: true,
    paysuccess: base.static + '/confirmorder/paysuccess.png',
    store_id: '',
    user_id: '', 
    redDeflag: 0,//红包
    order_suc_tk: base.static + '/redenvelopes/order_suc_fhbend.png',
    order_suc_hb_fbtn: base.static + '/redenvelopes/order_suc_hb_fbtn.png',
    cheap_status: 1,
    //分享 控制 
    share: {
      isShow: false,
      animate: false
    },
    user_info: {},
    activity: {},
    group: '',
    transmit: {
      style: 'width: 325rpx; height: 100rpx;font-size: 34rpx;color: #66CC66;text-align: center;line-height: 100rpx;',
      value: '去抽奖'
    },
    mask_box: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    let orderid = options.orid;
    let run_num = options.run_num;
    let user_info = wx.getStorageSync('user_info');
    let activity = wx.getStorageSync('activity');
    that.setData({
      orderid: orderid,
      run_num: run_num,
      user_info: user_info,
      activity: activity,
      user_id: user_info.id,
      store_id: options.store_id,
      segsid: options.segsid,
      share_url: options.share_url
    })

    that.storeListHot();//热销爆款
    that.getimgpath();
    that.sevenSignOrderHandle();//连续签到七天下单奖励弹框
    if (activity && activity.payorder_get_redpacket == 1) {
      shareInfoJson.getSendRedEnvelope(that, () => {
        that.setData({
          redDeflag: 1
        });
      });
    }
   
  },
  // 去抽奖
  goluck(){
    this.setData({
      mask_box:false
    })
    Router.go('luck')
  },
  // 一会再抽
  cancleluck() {
    this.setData({
      mask_box: false
    })
  },
 //获取今日支付订单数量
  getpay_order_num(){
    let that = this;
    service.joinactive({ type: 'get_day_pay_order_num' }, function (res) {
      if (res && res.code == 1) {
        if (res.data.today_prize_num <= 3 && !that.data.segsid) {
          that.setData({
            mask_box: true,
          })
        }
      }
    })
  },
  //今日热销
  storeListHot: function () {
    let that = this;
    let params = {
      page: 1,
      per_page: 10
    }
    service_store.customerRecommend(params, function (res) {
      if (res && res.code == 200) {
        that.setData({
          hottodyList: res.data.recommend,
          nomore: false
        });

      }
    })


  },
  
  //再逛逛
  gohome(){
   Router.go('store');
  },
  //查看订单
  goorderdetail(){
    let orderid = this.data.orderid;
    Router.go('orderDetail', `orid=${orderid}`);
  },
  //去我的群接龙
  gogroup(){
    Router.go('mygroup');
  },
  //去商品详情页
  goGoodsDetail(e){
    let that = this;
    let goodsid = e.currentTarget.dataset.good_id;
    let store_id = that.data.store_id;
    let hotdetail = 'hot';
    Router.go('detail', `goods_id=${goodsid}&store_id=${store_id}&hot=${hotdetail}`);
    that.viewRecord(goodsid, '', "view")
  },
  //获取图片路径
  getimgpath() {
    rewardsj.getBounty({}, res => {
      if (res && res.code == 1) {
        let data = res.data;
        if (data.config_img) {
          this.setData({
            config_img: data.config_img
          })
        } else {
          this.setData({
            config_img: shareInfoJson.cardInfo.imageUrl
          })
        }
      }
    });
  },
  //监听页面高度
  scrollTimer: null,
  onPageScroll: function (e) {
    let that = this;
    if (that.scrollTimer) {
      clearTimeout(that.scrollTimer)
    };
    that.scrollTimer = setTimeout(() => {
      that.setData({
        scrollTop: e.scrollTop
      })
    }, 50);
  },
  
  //红包
  closeredDe() {
    this.setData({
      redDeflag: 0
    });
    Router.go('myredinfo');
    
  },

  openshare: function (e) {
    if (this.data.cheap_status) {
      shareInfoJson.getbonusNum(this);
    } else {
      wx.showToast({
        title: '活动已结束！',
        icon: 'none',
        duration: 3000,
        complete: (res) => {
          setTimeout(() => {
            this.setData({
              redDeflag: 0
            });
          }, 1500)
        }
      })
    }
    this.viewRecord('','fahongbao','click')
  },
  goGonglue() {
    shareInfoJson.cardInfo.goGonglue();
  },

  //连续签到7天且24小时内首次下单有3元提示
  sevenSignOrderHandle(){
    let that = this;
    service.ifsignsuccess({},function(res){
      if(res && res.code ==1){
        if(res.data == 1){
          wx.showModal({
            title: '恭喜获得3元现金奖励！',
            content: '奖励金将在1个工作日内发放到您的余额账户，请注意查收哦',
            showCancel: false,
            success(res){
              if(res.confirm){
                that.getpay_order_num();//抽奖弹窗
              }
            }
          })
        }else if(res.data == 0){
          that.getpay_order_num();//抽奖弹窗          
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let that = this;
    let id = that.data.store_id;
    let user_id = wx.getStorageSync('user_info').id
    let paths = 'pages/store/index?store_id=' + that.data.store_id + '&to=' + encodeURIComponent('/pages/detail/dragon?segsid=' + that.data.segsid + '&sharerecord=groupDragoncard' + '&store_id=' + that.data.store_id + '&user_id=' + user_id);
    let imgurl = 'https:'+decodeURIComponent(that.data.share_url);
    return {
      title: '力度太大，我好害怕！多人购买，居然还会退差价！',
      path: paths,
      imageUrl: imgurl
    }
  }



    // let that = this;
    // let shareObj = {
    //   title: shareInfoJson.cardInfo.titlefn(),
    //   path: shareInfoJson.cardInfo.pathFn(this) + `&hb_sharePath=orderDetail`,
    //   imageUrl: that.data.config_img,
    //   success: function (res) {
    //     console.log(res, 'success');
    //   },
    //   fail: function (res) {
    //     console.log(res, 'fail');
    //   },
    //   complete: function () {
    //     console.log('complete');
    //   }
    // }
    // if(options.from == 'button'){
    //   shareObj.path = '/pages/my/joinshopman?shareType=1';
    // }
    // return shareObj;
 // }
})